<?php
declare (strict_types=1);

namespace app\common\model;

class Address extends Base
{
    protected $name = 'user_address';

    protected function p(): object
    {
        return $this->hasOne(Area::class, 'id', 'province')
            ->field('id,ext_name as title');
    }

    protected function c(): object
    {
        return $this->hasOne(Area::class, 'id', 'city')
            ->field('id,ext_name as title');
    }

    protected function d(): object
    {
        return $this->hasOne(Area::class, 'id', 'district')
            ->field('id,ext_name as title');
    }

    public static function add($param, $user): array
    {
        try {
            if (!empty($param['default']) && $param['default'] == 1) {
                parent::update(['default' => 0], ['uid' => $user->id]);
            } else {
                $param['default'] = 0;
            }
            if (!empty($param['id'])) {
                parent::update($param, ['id' => $param['id']]);
                $id = $param['id'];
            } else {
                $param['uid'] = $user->id;
                unset($param['type']);
                $new = parent::create($param);
                $id = $new->id;
            }

            return ['code' => 1, 'msg' => '保存成功', 'data' => $id];
        } catch (\Exception $e) {

            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function edit($id, $user): array
    {
        try {
            $info = parent::where(['id' => $id, 'uid' => $user->id])->find();
            if (empty($info)) {
                throw new \Exception('该地址不存在');
            }
            parent::destroy(['id' => $info->id], true);

            return ['code' => 1, 'msg' => '删除成功'];
        } catch (\Exception $e) {

            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function setDefault($id, $user): array
    {
        try {
            $info = parent::where(['id' => $id, 'uid' => $user->id])->find();
            if (empty($info)) {
                throw new \Exception('该地址不存在');
            }
            parent::update(['default' => 0], ['uid' => $user->id]);
            $info->default = 1;
            $info->save();

            return ['code' => 1, 'msg' => '设置成功'];
        } catch (\Exception $e) {

            return ['code' => -1, 'msg' => $e->getMessage()];
        }
    }

    public static function getDefault($uid): object|string
    {
        try {
            $info = parent::withoutField('create_time,update_time,delete_time')->where(['uid' => $uid, 'default' => 1])->find();
            if (!$info) {
                $info = parent::withoutField('create_time,update_time,delete_time')->where(['uid' => $uid])
                    ->order('update_time desc')->find();
                if (!$info) return '';
            }
            if ($info) {
                $p = $info->p ? $info->p->title : '';
                $c = $info->c ? $info->c->title : '';
                $d = $info->d ? $info->d->title : '';
                $info->address = $p . $c . $d;
                unset($info->p);
                unset($info->c);
                unset($info->d);
            }

            return $info;
        } catch (\Exception $e) {

            return '';
        }
    }
}